1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.
event.*;
4 import java.util.regex.*;

5
6 class
AddFrame extends JFrame
7 {
8     Container c;
9     JLabel lblRno, lblName;
10     JTextField txtRno,txtName;
11     JButton btnSave, btnBack;
12     JPanel p1,p2;
13
14     AddFrame()
15     {
16         c =getContentPane();
17         c.setLayout(
new BoxLayout(c , BoxLayout.Y_AXIS));
18         c.setBackground(Color.orange);
19         
20         p1 =
new JPanel();
21         lblRno =
new JLabel("Rno:");
22         txtRno =
new JTextField(4);
23         lblName =
new JLabel("Name:");
24         txtName =
new JTextField(10);
25
26         p1.
add(lblRno);
27         p1.
add(txtRno);
28         p1.
add(lblName);
29         p1.
add(txtName);
30         p1.setBackground(Color.orange);
31         c.
add(p1);
32
33         p2 =
new JPanel();
34         btnSave =
new JButton("Save");
35         btnSave.setBackground(Color.orange);
36         btnBack =
new JButton("Back");
37         btnBack.setBackground(Color.orange);
38         p2.
add(btnSave);
39         p2.
add(btnBack);
40         c.
add(p2);
41     
42         btnSave.addActionListener(
new ActionListener(){
43             
public void actionPerformed(ActionEvent ae) {
44                 
try
45                 {
46                 String rno = txtRno.getText();
47                 String name = txtName.getText();
48                 System.
out.println(name);
49                 DbHandler db =
new DbHandler();
50                 
if( txtName.getText().isEmpty())
51                 {
52                     
throw new InvalidFieldException();
53                 }
54                 
if(Integer.parseInt(rno)<0)
55                 {
56                     
throw new NumberGreaterException();
57                 }
58                 
if(Pattern.matches("[a-zA-Z]+", name) == false){
59                     
throw new Exception();
60                 }
61                 
//if
62                 
//throw new StringOnlyException("name");
63
64                 db.addStudent(Integer.parseInt(rno),name);
65                 }
66                 
catch(NumberFormatException nfe)
67                 {
68                     JOptionPane.showMessageDialog(
new JDialog(), "Please Enter Integer");
69                 }
70                 
catch(NumberGreaterException nge)
71                 {
72                     JOptionPane.showMessageDialog(
new JDialog(), "Number should be greater than 0");
73                 }
74                 
catch(InvalidFieldException iae)
75                 {
76                     JOptionPane.showMessageDialog(
new JDialog(), "Field(s) should not be blank");
77                 }
78                 
catch(Exception e)
79                 {
80                     JOptionPane.showMessageDialog(
new JDialog(), "Name should not contain Integers");
81                 }
82                 
/*
83                 
catch(StringOnlyException soe)
84                 {
85                     JOptionPane.showMessageDialog(
new JDialog(), "Number not allowed");
86                 }
87                 */

88             }});
89     
90         btnBack.addActionListener(
new ActionListener(){
91             
public void actionPerformed(ActionEvent ae){
92                 MainFrame a =
new MainFrame();
93                 dispose();
94             }});
95     
96
97
98         setTitle(
"Add Student");
99         setSize(
500,400);
100         setLocationRelativeTo(
null);
101         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
102         setVisible(
true);
103
104         }
105 }
// end of class Addframe
106
107 class
NumberGreaterException extends Exception
108 {
109 }

110
111 class
InvalidFieldException extends Exception
112 {
113 }

114
115 /*

116 class
StringOnlyException extends Exception
117 {
118     
public StringOnlyException(String s)
119     {
120         //name =
this.name;
121         super(s);
122         //JOptionPane.showMessageDialog(
new JDialog(), name);
123     }
124 }
125 */


Gõ tìm kiếm nhanh...